﻿<!DOCTYPE html>
<html lang="zh_CN">

<head>
    <title>图案填充</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="图形系统开发实战:进阶篇 示例">
    <meta name="author" content="hjq">
    <meta name="keywords" content="canvas,anygraph,javascript">
    <!-- frame所需脚本和样式 -->
    <link rel="stylesheet" href="../../script/bootstrap-3.3.5/css/bootstrap.min.css">   
    <link rel="stylesheet" href="../../script/codemirror/codemirror.css">
    <script src="../../script/lib/jquery-1.11.2.min.js"></script>
    <script src="../../script/codemirror/codemirror_merge.js"></script>
    <!-- 加载代码块 -->
	<link rel="stylesheet" href="../css/styles.css">
    <script src="../js/helper.js"></script>
    <script>
        var pageTitle = "图案填充";
        var pageDesc = "使用图案填充效果填充几何形状";
    </script>
</head>

<body style="margin:10px;">
    <div id="graphWrapper" data-type="graph" style="width:850px; height:420px; border:solid 1px #CCC;"></div>
    <div style="margin:10px; text-align: left;">
        <div class="checkbox" style="font-size: 16px;">
            <label style="margin-right:20px"><input id="chkRect" type="checkbox">矩形</label>
            <label style="margin-right:20px"><input id="chkCircle" type="checkbox">圆形</label>
        </div>
    </div>
</body>
<script type="module">
    import {
        Graph, Layer, VectorSource, BgUtil, Color, MathUtil, ImageObject,
        GGeometryType, Rect, Point, Circle, Pattern, Text, Polyline, Polygon
    } from "../../src/index.js";

    let colorSet = ["red", "orange", "gold", "green", "black", "blue"];

    // graph对象
    let graph = new Graph({
        "target": "graphWrapper"
    });
    
    let pattern = [];
    for(let i=0; i<8; i++) {
		pattern.push(new Pattern({"type":"image", "image":new ImageObject("../images/bg_0" + (i+1) + ".jpg")}));
	}

    // 网格水印层
    let bgLayer = BgUtil.generateGrid(Object.assign({ "interval": 10, "graph": graph }, graph.getSize()));
    bgLayer.getSource().add(new Text({
        "text": "图案填充 Demo",
        "x": graph.getSize().width / 2,
        "y": graph.getSize().height / 2,
        "vectorSize": false,
        "style": { "lineWidth": 4, "fillStyle": 0, "fillColor": "#D0D0D0", "fontSize": 30, "fontName": "黑体", "textAlign": "center", "textBaseline": "middle" }
    }));

    // 新建绘图图层
    let layer = graph.addLayer();

    // pageload
    $(document).ready(function () {
		let idx = 0;
        $("#chkRect").on("change", function () {
            if ($(this).prop("checked")) {
                let width = 180;
                let height = 160;
                for (let x = 40; x < 800; x += width + 20) {
                    for (let y = 40; y < 400; y += height + 20) {
                        layer.getSource().add(new Rect({
                            x, y, width, height, "style": { "fillColor": pattern[idx % 8], "fillStyle": 1, "color": "none" }
                        }));
                        idx ++;
                    }
                }
            } else {
                layer.getSource().clearTypeData(GGeometryType.RECT);
            }
            graph.render();
        });

        $("#chkCircle").on("change", function () {
            if ($(this).prop("checked")) {
                let radius = 100;
                for (let x = 150; x < 800; x += radius * 2.5) {
                    for (let y = 130; y < 500; y += radius * 2.25) {
                        layer.getSource().add(new Circle({
                            x, y, radius,
                            "style": { "fillColor": pattern[idx % 8], "fillStyle": 1, "color": "none" }
                        }));
                        idx ++;
                    }
                }
            } else {
                layer.getSource().clearTypeData(GGeometryType.CIRCLE);
            }
            graph.render();
        });
        
        // $("#chkRect").click();
    })

</script>

</html>